using System;

namespace Toolset {	
	public class ConversationBlogger {
		public System.Collections.Hashtable Conversations = new System.Collections.Hashtable();
		

		public System.Timers.Timer PublishTimer = new System.Timers.Timer(System.TimeSpan.FromHours(1).TotalMilliseconds);
		public ConversationBlogger() {

			PublishTimer.AutoReset=true;
			PublishTimer.Elapsed+=new System.Timers.ElapsedEventHandler(PublishTimer_Elapsed);
			PublishTimer.Start();
		}
		public void RecordParagraph(string Name, string Paragraph, bool In) {
			System.Collections.ArrayList ConversationList=null;
			if(Conversations.ContainsKey(Name)) {
				ConversationList = (System.Collections.ArrayList)Conversations[Name];
			}else {
				ConversationList = new System.Collections.ArrayList();
				Conversations.Add(Name, ConversationList);
			}			
			ConversationList.Add(new Conversation(Name, Paragraph, In));
		}

		private void PublishTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
			PublishTimer.Stop();
			try {
				System.Collections.ArrayList ListOfRemoves = new System.Collections.ArrayList();
				foreach(string Name in Conversations.Keys) {
					System.Collections.ArrayList ConversationList = (System.Collections.ArrayList)Conversations[Name];
					Conversation lastEntry = (Conversation)ConversationList[ConversationList.Count-1];
					System.TimeSpan diff = new System.TimeSpan(System.DateTime.Now.Ticks-lastEntry.When.Ticks);
					if(diff.TotalMilliseconds>System.TimeSpan.FromHours(1).TotalMilliseconds) {
						PostConversation(Name, ConversationList);
						ListOfRemoves.Add(Name);
					}
				}
				foreach(string name in ListOfRemoves) {
					Conversations.Remove(name);
				}
			}catch(Exception exc) {
				exc.ToString();
			}
			PublishTimer.Start();
		}
		private void PostConversation(string Name, System.Collections.ArrayList ConversationList) {
			System.Text.StringBuilder body = new System.Text.StringBuilder();
			foreach(Conversation conv in ConversationList) {
                string name = Plugins.PluginLoader.ScreenName;
				if(conv.In) name = conv.Name;
				body.Append("<b><i>" + conv.When + "</i>:" + name + "</b>:"+conv.Paragraph+"<br />");
			}
			string subject = "A conversation I just had with " + Name;
			 
			Unified.Network.Atom.AtomEngine engine = new Unified.Network.Atom.AtomEngine("anikablog", "blogpass");
			Unified.Network.Atom.Entry entry = new Unified.Network.Atom.Entry(subject, body.ToString(), System.DateTime.Now);
			engine.PostEntry("https://www.blogger.com/atom/15456651", entry);

			
		}
	}
}
